﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GM.Model.SubSonic;

namespace GM.Forms
{
    public partial class MoveAssetForm : Form
    {
        private bool m_reason;
        private gm_srodki_trwale m_asset;
        private readonly List<Pracownik> m_pracownicy;
        private readonly List<sale> m_sale;

        public MoveAssetForm(IEnumerable<Pracownik> pracownicy, IEnumerable<sale> sale)
        {
            m_pracownicy = new List<Pracownik>(pracownicy);
            m_sale = new List<sale>(sale);

            InitializeComponent();

            rbtnPersonal.Checked = true;
        }

        private void MoveAssetForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (!m_reason)
                return;

            if (rbtnPersonal.Checked)
            {
                var pracownikId = m_pracownicy.First(
                    x =>
                    string.Format("{0} {1}", x.imie, x.nazwisko) ==
                    cbxDestination.SelectedItem.ToString()).id_prac;

                var move = new gm_srodki_trwale_ruchy
                               {
                                   data_przekazania = DateTime.Today.ToShortDateString(),
                                   srodek_id = m_asset.nr_ewidencyjny,
                                   pracownik_id = pracownikId

                               };
                move.Save();
                m_asset.osoba_odpowiedzialna = pracownikId;
                m_asset.Save();
            }
            else
            {
                var move = new gm_srodki_trwale_ruchy
                {
                    srodek_id = m_asset.nr_ewidencyjny,
                    data_przekazania = DateTime.Today.ToShortDateString(),
                    numer_sali = int.Parse(cbxDestination.SelectedItem.ToString())
                };
                move.Save();
                m_asset.umiejscowienie = int.Parse(cbxDestination.SelectedItem.ToString());
                m_asset.Save();
            }

            ClearForm();
            Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
            Hide();
        }

        private void tbxReason_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxReason.Text.Trim()))
            {
                errorProvider.SetError(tbxReason, "Uzasadnienie nie może być puste.");
                m_reason = false;
            }
            else
            {
                errorProvider.SetError(tbxReason, string.Empty);
                m_reason = true;
            }
        }

        public void MoveAsset(gm_srodki_trwale asset)
        {
            if (asset == null)
                return;
            m_asset = asset;
            Show();
            BringToFront();
        }

        private void ClearForm()
        {
            tbxReason.Text = string.Empty;
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            if(rbtnPersonal.Checked)
            {
                cbxDestination.Items.Clear();
                cbxDestination.Items.AddRange(
                    m_pracownicy.Select(x => string.Format("{0} {1}", x.imie, x.nazwisko)).ToArray());
            }
            else
            {
                cbxDestination.Items.Clear();
                cbxDestination.Items.AddRange(m_sale.Select(x => x.numer.ToString()).ToArray());
            }
        }
    }
}
